package org.cunframework.sc.map4j.support.handle.blank;

import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;

import java.awt.image.BufferedImage;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.cunframework.modules.utils.FileUtil;

/**
 * 当请求的瓦片不存在时反回一个默认的LOGO标识图
 * 
 * <p/>
 * <p>User: weiq
 * <p>Date: 2015年10月27日 下午5:44:00 
 * <p>Version: 1.0
 */
public class BlankImageHandler implements HttpHandler {
    public BufferedImage bi = null;
    public BlankImageHandler() {
        bi = FileUtil.base64ToImage(BLANK_IMG_BASE64);
    } 

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        exchange.startBlocking();
        OutputStream out = exchange.getOutputStream();
        ImageIO.write(bi, "png", out);
        IOUtils.closeQuietly(out);
    }
    
    private static final String BLANK_IMG_BASE64 = "R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
}
